<template>
  <view
    class="form-box"
    :data-theme="cueTheme"
  >
    <custom-navbar title="修改密码" />
    <basic-components />
    <m-form
      :options="addForm.options"
      :editable="addForm.editable"
      @get-model="form => (addForm.form = form)"
      @submit="save"
      @save="formSave => (addForm.save = formSave)"
    />
    <view class="bottom-add">
      <view>
        <back-page />
      </view>
      <view
        class="left"
        @tap="addForm.save"
      >
        完成
      </view>
    </view>
  </view>
</template>

<script>
import { userUpPasswordByUser } from "@/config/api/index"

export default {
  // 标题右侧按钮
  onNavigationBarButtonTap: function (e) {
    this.$refs.uForm.validate().then(res => {
      this.save()
    })
  },
  onLoad(options) {
    this.init(options)
  },
  onShow() {},
  data() {
    return {
      addForm: {
        form: null,
        options: [
          {
            type: "input",
            label: "账号",
            prop: "username",
            attrs: {
              disabled: true,
              clearable: true
            },
            rules: [
              {
                required: true,
                errorMessage: "用户名称不能为空"
              }
            ]
          },
          {
            type: "input",
            label: "旧密码",
            prop: "oldPassword",
            attrs: {
              disabled: false,
              clearable: true,
              type: "password"
            },
            rules: [
              {
                required: true,
                errorMessage: "旧密码不能为空"
              }
            ]
          },
          {
            type: "input",
            label: "新密码",
            prop: "newPassword",
            attrs: {
              disabled: false,
              clearable: true,
              type: "password"
            },
            rules: [
              {
                required: true,
                errorMessage: "新密码不能为空"
              }
            ]
          }
        ],
        save: null
      }
    }
  },

  methods: {
    async init() {
      setTimeout(() => {
        this.addForm.form.username = uni.getStorageSync("login_name") || ""
      }, 0)
    },
    async save() {
      try {
        await userUpPasswordByUser(this.addForm.form)
        this.appMessage("修改成功 即将返回登录页面")

        setTimeout(() => {
          uni.redirectTo({
            url: "/pages/login/login"
          })
        }, 1500)
      } catch (error) {
        this.appMessage("修改成功 即将返回登录页面", 2)
      }
    }
  }
}
</script>
